/**
 *  This class is the main class of the "World of Zuul" application. 
 *  "World of Zuul" is a very simple, text based adventure game.  Users 
 *  can walk around some scenery. That's all. It should really be extended 
 *  to make it more interesting!
 * 
 *  To play this game, create an instance of this class and call the "play"
 *  method.
 * 
 *  This main class creates and initialises all the others: it creates all
 *  rooms, creates the parser and starts the game.  It also evaluates and
 *  executes the commands that the parser returns.
 * 
 * @author  Michael K?lling and David J. Barnes
 * @version 2016.02.29
 */

public class Game 
{
    private Parser parser;
    private Room currentRoom;
    private Player player;
    private boolean isAdopt=false;
    private boolean ending=false;
    private int steps = 0;
    /**
     * Create the game and initialise its internal map.
     */
    public Game() 
    {
        createRooms();
        parser = new Parser();
        player = new Player();
    }

    /**
     * Create all the rooms and link their exits together.
     */
    private void createRooms()
    {
        Room room1,room2,room3,room4,room5,room6,room7,room8,room9,room10,room2_w,room2_e,room4_e,room4_w;
        Room room5_w,room5_e,room7_w,room7_e,room9_w,room9_e;
        // create the rooms
        room1 = new Room("第一层?");
        room2 = new Room("第二层");
        room3 = new Room("第三层");
        room4 = new Room("第四层");
        room5 = new Room("第五层");
        room6 = new Room("第六层");
        room7 = new Room("第七层");
        room8 = new Room("第八层");
        room9 = new Room("第九层");
        room10 = new Room("第十层");
        
        room2_w = new Room("动物园",new Thing("香蕉剑",20,"攻击装",5));
        room2_e = new Room("药店",new Thing("小补药",30));
        
        room4_w = new Room("铁匠铺",new Thing("铁剑",-10,"攻击装",10));
        room4_e = new Room("水果摊",new Thing("大苹果",40));
        
        room5_w = new Room("战士的家",new Thing("叠甲",-10,"防御装",15));
        room5_e = new Room("金铺",new Thing("金色的苹果",100,"攻击装",20));
        
        room7_w = new Room("金华职业技术学院",new Thing("冰霜之心",-30,"防御装",25));
        room7_e = new Room("鲍勃的店",new Thing("香辣饼干",50));
        
        room9_w = new Room("决战补给",new Thing("漏斗蛋糕",70));
        room9_e = new Room("恶魔传送门",new Thing("噩梦剑",50,"攻击装",25));
        
        // initialise room exits
        //outside.setExits(null, theater, lab, pub);
        // outside.setExit("east", theater);
        // outside.setExit("south", lab);
        // outside.setExit("west", pub);
        room1.setExit("north" ,room2);
        
        room2.setExit("south", room1);
        room2.setExit("north" ,room3);
        
        room3.setExit("south", room2);
        room3.setExit("north" ,room4);
        
        room4.setExit("south", room3);
        room4.setExit("north" ,room5);
        
        room5.setExit("south", room4);
        room5.setExit("north", room6);
        
        room6.setExit("south" ,room5);
        room6.setExit("north", room7);
        
        room7.setExit("south" ,room6);
        room7.setExit("north", room8);
        
        room8.setExit("south" ,room7);
        room8.setExit("north", room9);
        
        room9.setExit("south" ,room8);
        room9.setExit("north", room10);
        
        room10.setExit("south", room9);
        // theater.setExits(null, null, null, outside);
        // pub.setExits(null, outside, null, null);
        // lab.setExits(outside, office, null, null);
        // office.setExits(null, null, null, lab);
        room2_e.setExit("west", room2);
        room2.setExit("east", room2_e);
        room2_w.setExit("east", room2);
        room2.setExit("west", room2_w);
        
        room4_e.setExit("west", room4);
        room4.setExit("east", room4_e);
        room4_w.setExit("east", room4);
        room4.setExit("west", room4_w);
       
        room5_e.setExit("west", room5);
        room5.setExit("east", room5_e);
        room5_w.setExit("east", room5);
        room5.setExit("west", room5_w);
        
        room7_e.setExit("west", room7);
        room7.setExit("east", room7_e);
        room7_w.setExit("east", room7);
        room7.setExit("west", room7_w);
        
        room9_e.setExit("west", room9);
        room9.setExit("east", room9_e);
        room9_w.setExit("east", room9);
        room9.setExit("west", room9_w);
        currentRoom = room1;  // start game outside
    }

    /**
     *  Main play routine.  Loops until end of play.
     */
    public void play() 
    {            
        printWelcome();

        // Enter the main command loop.  Here we repeatedly read commands and
        // execute them until the game is over.
                
        boolean finished = false;
        while (! finished) {
            Command command = parser.getCommand();
            finished = processCommand(command);
        }
        // System.out.println("魔王被困在城市 还需要你的再次帮助");
    }

    /**
     * Print out the opening message for the player.
     */
    private void printWelcome()
    {
        System.out.println();
        System.out.println("两年前，马尔加内斯魔王的上古宝石遭冒险者窃取。");
        System.out.println("但是大魔王的记性好像不怎么好，现在才发现东西丢了。");
        System.out.println("他决定去找冒险者复仇，谁让他是一个脾气暴躁的魔头呢！");
        System.out.println("他来到了冒险者的城市。");
        System.out.println("人类的城市对于魔王来说就像一个  迷宫   ");
        System.out.println("魔王在第一层 他还需要上去9层");
        System.out.println("你需要操作魔王在主干道路上经过10个房间去打败最后的勇者");
        System.out.println("当然必须保证魔王的存活(指血量大于0)，你的每次操作都会消耗魔王的血量");
        System.out.println("每当魔王进入的一个房间 都有可能遇到房间的主人在家里 这是就会触发战斗 也可能运气比较好 房间里都没人");
        System.out.println("主干道路以外的房间里会有道具可用 使用eat 或者 equip");
        System.out.println("魔王怎么能独自行动呢？");
        System.out.println("魔王启动了召唤法阵！");
        System.out.println("但是魔王被人类城市中的魔法阵限制，只能消耗少量法力值随机召唤恶魔仆从！");
        System.out.println("魔王可没有好脾气 当他的血量低于50时会连续发动两次攻击！！");
        System.out.println("输入'adopt+仆从名称'即可随机获得一个强(rou)大(xiao)的仆从！");
        currentRoom.printExits();
    }

    /**
     * Given a command, process (that is: execute) the command.
     * @param command The command to be processed.
     * @return true If the command ends the game, false otherwise.
     */
    private boolean processCommand(Command command) 
    {
        boolean wantToQuit = false;
        if(command.isUnknown()) {
            System.out.println("不能理解的语言");
            return false;
        }

        Word commandWord = command.getCommandWord();
        // if (commandWord.equals("help")) {
            // printHelp();
        // }
        // else if (commandWord.equals("go")) {
            // goRoom(command);
        // }
        // else if (commandWord.equals("quit")) {
            // wantToQuit = quit(command);
        // }
        // else if (commandWord.equals("look")) {
            // lookRoom();
        // }
        // else if (commandWord.equals("check")) {
            // player.checkBag();
        // }
        // else if (commandWord.equals("pick")) {
            // pickThing();
        // }
        // else if (commandWord.equals("eat")) {
            // eatThing(command);
        // }
        // else if (commandWord.equals("Hp")) {
            // ;
        // }
        switch(commandWord){
            case HELP:
                printHelp();
                break;
            case GO:
                goRoom(command);
                wantToQuit = player.isDead();
                break;
            case QUIT:
                wantToQuit = quit(command);
                break;
            case PICK:
                pickThing();
                break;
            case EAT:
                eatThing(command);
                break;
            case LOOK:
                lookRoom();
                break;
            case CHECK:
                player.checkBag();
                break;
            case ADOPT:
                adoptPet(command,player);
                break;
            case EQUIP:
                equipThing(command);
                break;
            case SHOW:
                show();
                break;
        }
        if(ending) wantToQuit = true;
        return wantToQuit;
    }

    // implementations of user commands:

    /**
     * Print out some help information.
     * Here we print some stupid, cryptic message and a list of the 
     * command words.
     */
    private void printHelp() 
    {
        System.out.println("你和你的仆从独自在人类城堡里十分孤独!");
        System.out.println("但是你复仇的欲望正在高涨!");
        System.out.println();
        System.out.println("你可以输入的按键有");
        System.out.println("go quit help look check pick eat equip show");
    }

    /** 
     * Try to go in one direction. If there is an exit, enter
     * the new room, otherwise print an error message.
     */
    private void goRoom(Command command) 
    {
        if(!command.hasSecondWord()) {
            // if there is no second word, we don't know where to go...
            System.out.println("去哪？");
            return;
        }

        String direction = command.getSecondWord();

        // Try to leave current room.
        Room nextRoom = currentRoom.goNext(direction);

        if (nextRoom == null) {
            System.out.println("无法进入！");
        }
        else {
            steps++;
            currentRoom = nextRoom;
            System.out.println("你在" + currentRoom.getDescription());
            player.step();
            currentRoom.printExits();
            int ran=(int)(Math.random()*100);
            if(ran<=70)
            {
                Monster monster=new Monster(steps);
                monster.show();
                new Thread(){
                public void run(){
                try {
                Thread.sleep(2000);
                System.out.println("开始战斗");
                Thread.sleep(500);
                Battle battle = new Battle();
                battle.battles(player,monster);
                } catch (InterruptedException e) {}
                }
                }.start();
            }
            if(currentRoom.getDescription() == "第十层")
            {
                System.out.println("最后的boos战斗,你将挑战这个国家最最最厉害的勇者(应该吧)");
                Monster monsterz = new Monster("最最最厉害的勇者",steps);
                new Thread(){
                public void run(){
                try {
                Thread.sleep(5000);
                System.out.println("开始战斗");
                Thread.sleep(500);
                Battle battle = new Battle();
                battle.battles(player,monsterz);
                } catch (InterruptedException e) {}
                }
                }.start();
                player.isDead("ending");
                ending = true;
            }
        }
    }
    private void adoptPet(Command command,Player player)
    {
        if(isAdopt)
        {
            System.out.println("已经有恶魔仆从了了，不能继续召唤了!");
            return;
        }
        if(!command.hasSecondWord()) {
            // if there is no second word, we don't know where to go...
            System.out.println("没有名字哦");
            return;
        }
        Pet pet = new Pet(command.getSecondWord());
        pet.showPet();
        isAdopt=true;
        pet.effect(player);
    }
    private void lookRoom()
    {
        if(currentRoom.getThing()==null)
            System.out.println("这里没东西");
        else System.out.println("这里有"+currentRoom.getThing().getName()+"能量值:"+currentRoom.getThing().getEnergy());
    }
    private void show()
    {
        player.show();
    }
    private void pickThing()
    {
        player.pick(currentRoom.getThing());
        currentRoom.remove();
    }
    private void eatThing(Command command)
    {
        if(!command.hasSecondWord()) {
            // if there is no second word, we don't know where to go...
            System.out.println("吃什么东西？");
            return;
        }
        player.eat(command.getSecondWord());
    }
    private void equipThing(Command command)
    {
        if(!command.hasSecondWord()) {
            // if there is no second word, we don't know where to go...
            System.out.println("饭可以乱吃 装备不能乱穿");
            return;
        }
        player.equip(command.getSecondWord());
    }
    /** 
     * "Quit" was entered. Check the rest of the command to see
     * whether we really quit the game.
     * @return true, if this command quits the game, false otherwise.
     */
    private boolean quit(Command command) 
    {
        if(command.hasSecondWord()) {
            System.out.println("退出什么?");
            return false;
        }
        else {
            return true;  // signal that we want to quit
        }
    }
    private boolean quit() 
    {
            return true;  // signal that we want to quit
    }
}